<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工作地点" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入工作地点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家" prop="countryRegion">
        <el-select v-model="queryParams.countryRegion" placeholder="请选择">
            <el-option
               v-for="dict in country"
               :key="dict.value"
               :label="dict.label"
               :value="dict.label"
            ></el-option>
         </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['zuzhi:location:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zuzhi:location:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="locationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工作地点名称" align="center" prop="locationName" />
      <el-table-column label="国家/地区" align="center" prop="countryRegion" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="邮政编码" align="center" prop="postalCode" />
      <el-table-column label="类型" align="center" prop="locationType" />
      <el-table-column label="员工数量" align="center" prop="employeeCount" />
      <el-table-column label="地点描述信息" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['zuzhi:location:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['zuzhi:location:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司工作地点信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="locationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工作地点名称" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入工作地点名称" />
        </el-form-item>
        <el-form-item label="国家/地区" prop="countryRegion">
          <el-select v-model="form.countryRegion" placeholder="请选择">
              <el-option
                 v-for="dict in country"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.label"
              ></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="所在城市" prop="city">
          <el-cascader
            v-model="form.city"
            :options="regionOptions"
            :props="{ expandTrigger: 'hover' }"
            placeholder="请选择省/市/区"    style="width: 100%"
            @change="handleCityChange"
          />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="postalCode">
          <el-input v-model="form.postalCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="员工数量" prop="employeeCount">
          <el-input v-model="form.employeeCount" placeholder="请输入员工数量" />
        </el-form-item>
        <el-form-item label="地点描述信息" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Location">
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/zuzhi/location"

const { proxy } = getCurrentInstance()
const { country } = proxy.useDict("country");

const locationList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const regionOptions = ref([
  {
    value: 'zhejiang',
    label: '浙江省',
    children: [
      {
        value: 'hangzhou',
        label: '杭州市',
        children: [
          { value: 'yuhang', label: '余杭区' },
          { value: 'xihu', label: '西湖区' }
        ]
      },
      {
        value: 'ningbo',
        label: '宁波市',
        children: [
          { value: 'haishu', label: '海曙区' },
          { value: 'jiangbei', label: '江北区' }
        ]
      }
    ]
  },
  {
    value: 'shanghai',
    label: '上海市',
    children: [
      { value: 'pudong', label: '浦东新区' },
      { value: 'jingan', label: '静安区' }
    ]
  }
])

const data = reactive({
  form: {
    city: null,
    cityLabel: null,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    locationName: null,
    countryRegion: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    locationName: [
      { required: true, message: "工作地点名称不能为空", trigger: "blur" }
    ],
    countryRegion: [
      { required: true, message: "国家/地区不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "所在城市不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

function handleCityChange(value) {
  const labels = []
  let node = regionOptions.value.find(p => p.value === value[0])
  if (node) {
    labels.push(node.label)
    node = node.children?.find(c => c.value === value[1])
    if (node) {
      labels.push(node.label)
      node = node.children?.find(g => g.value === value[2])
      if (node) {
        labels.push(node.label)
      }
    }
  }
  form.value.cityLabel = labels.join('>')
}


/** 查询公司工作地点信息列表 */
function getList() {
  loading.value = true
  listLocation(queryParams.value).then(response => {
    console.log(response)
    locationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    posId: null,
    locationName: null,
    countryRegion: null,
    city: null,
    address: null,
    postalCode: null,
    locationType: null,
    employeeCount: null,
    description: null,
    delFlag: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("locationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.posId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加公司工作地点信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _posId = row.posId || ids.value
  getLocation(_posId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改公司工作地点信息"
  })
}

/** 提交按钮 */
function submitForm() {
  form.value.city = form.value.cityLabel;
  proxy.$refs["locationRef"].validate(valid => {
    if (valid) {
      if (form.value.posId != null) {
        updateLocation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLocation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _posIds = row.posId || ids.value
  proxy.$modal.confirm('是否确认删除公司工作地点信息编号为"' + _posIds + '"的数据项？').then(function() {
    return delLocation(_posIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('zuzhi/location/export', {
    ...queryParams.value
  }, `location_${new Date().getTime()}.xlsx`)
}

getList()
</script>
